var base = document.getElementById("base").href;
d3.select(self.frameElement).style("height", "1600px");
var chooseTime = "${chooseTime}";

$(".chosen-select").chosen();
$(".chosen-select-deselect").chosen({ allow_single_deselect: true });

//点击表格更改颜色
function showDetail(value, obj, productId, good, medium, bad){

    openPartialLayer($("#lineChartDiv"));
    openPartialLayer($("#pieChartDiv"));

    var chooseTime = $("#chooseTime").val();
    var startTime = '';
    var endTime = '';
    if(chooseTime){
        startTime = chooseTime.split("-")[0];
        endTime = chooseTime.split("-")[1] + ' 23:59:59';
    }
    $("#product_detail").show();
    $(".proname").html(value);

    $(".product_tr tr").css("background-color","");
    $(obj).css("background-color","#d9edf6");
    //查询产品满意度
    $.ajax({
        url : base + '/comment/selectCommentTendency',
        data:
            {
                productId:productId,
                startTime:startTime,
                endTime:endTime
            },
        async: false,
        type: 'GET',
        dataType:'json',
        success:function(result){
            	var compare = function(a,b){
    				var pa=3;
    				var pb=3;
    				if(a.type=="好评"){
    					pa=1;
    				}else if(a.type=="中评"){
    					pa=2;
    				}
    				if(b.type=="好评"){
    					pb=1;
    				}else if(b.type=="中评"){
    					pb=2;
    				}
    				if(a.month!=b.month){
    					return a.month>b.month?1:-1;
    				}else{
    					return pa>pb?1:-1;
    				}
    			}
            	var	exportForm={
						'headers':  ['日期','评论等级','占比'],
						'cols':	    ['month', 'type', 'rate'],
						'compareMethod': compare
				};
            lineChartModal(result, 'lineChart', 'month', 'rate', 'type', '', '', '320', "",exportForm);
        }, error: function (XMLHttpRequest) {
            console.log(XMLHttpRequest);
        }
    });

    var arr1 = [];
    arr1.push({name: "好评", value: good});
    arr1.push({name: "中评", value: medium});
    arr1.push({name: "差评", value: bad});
    pieChartModal(arr1,"pieChart","name","value", 350);

    removeLoading($("#lineChartDiv"));
    removeLoading($("#pieChartDiv"));
}
//分页查询
function showPage(currentPage) {
    if(!isNull(currentPage) && typeof currentPage == "string" && currentPage.indexOf(",") != -1) {
        currentPage = currentPage.replace(",", "");
    }
    query(currentPage);
}

function isNull(str) {
    if(null == str || "" == str || (typeof str == "string" && str.trim() == "")) {
        return true;
    }
    return false;
}

var query = function (currentPage) {

    showPartialLayer();
    var chooseTime = $("#chooseTime").val();
    var productId = $("#product_id").val();
    var categoryId = $("#categoryId").val();
    var startTime = chooseTime.split("-")[0];
    var endTime = chooseTime.split("-")[1] + ' 23:59:59';
    var selectedTime = $('#selectedTime').val();
    var orderBy = $("#orderBy").val();
    var flag = $("#flag").val();
    var returnUrl = $("#returnUrl").val();
    //请求查询url
    var url = '/productComment/productAnalysis?currentPage=' + currentPage
        +'&startTime='+ startTime + '&endTime=' + endTime + '&orderBy=' + orderBy;
    if(productId){
        url += '&productId=' + productId;
    }
    if(categoryId){
        url += '&categoryId=' + categoryId;
    }
    if(chooseTime){
        url += '&chooseTime=' + chooseTime;
    }
    if(selectedTime){
        url += '&selectedTime=' + selectedTime;
    }
    if(flag){
        url += '&flag=' + flag;
        if(flag==='1' && returnUrl){
            url += "&returnUrl=" + returnUrl;
        }
    }
    window.location.href = base + url;
};

function showPartialLayer() {
    openPartialLayer($("#dynamic-table_wrapper"));
    var pro1dis = $("#product_detail").css("display");
    if(pro1dis != null && pro1dis != "none") {
        openPartialLayer($("#lineChartDiv"));
        openPartialLayer($("#pieChartDiv"));
    }
}

//搜索
$('#productSearch').click(function () {
    query(1);
});
//排序
$('.sortingProduct').click(function () {
    var attr = $(this).attr("attr");
    var sortCls = $(this).attr("class");
    if(sortCls.indexOf("sorting_asc") !== -1){
        $(this).removeClass("sorting_asc");
        $(this).addClass("sorting_desc");
        $("#orderBy").val(attr + " desc");
        query(1);
        return;
    }
    if(sortCls.indexOf("sorting_desc") !== -1){
        $(this).removeClass("sorting_desc");
        $(this).addClass("sorting_asc");
        $("#orderBy").val(attr + " asc");
        query(1);
        return;
    }
    if(sortCls.indexOf("sorting") !== -1){
        $(this).removeClass("sorting");
        $(this).addClass("sorting_desc");
        $("#orderBy").val(attr + " desc");
        query(1);
    }
    query(1);
});

$("#chooseTime").change(function () {
    $(".btn.btn-white.btn-sm.btn-info").removeClass("btn-info");
    $('#selectedTime').val("");
});
window.onload = function() {
    //选中时间回显
    if(initSelectedTime){
        if(initSelectedTime === '1'){
            replaceDate($('#dayBtn'),'1');
            $('#selectedTime').val('1');
        }else if(initSelectedTime === '7'){
            replaceDate($('#weekBtn'),'7');
            $('#selectedTime').val('7');
        }else if(initSelectedTime === '31'){
            replaceDate($('#monthBtn'),'31');
            $('#selectedTime').val('31');
        }else if(initSelectedTime === '365'){
            setCustomChooseTime(365);
        }else if(initSelectedTime === '15'){
            setCustomChooseTime(15);
        }
    }
    $(".product_tr tr:first-child").click();
};
var setCustomChooseTime = function(days){
    $('#selectedTime').val('' + days);
    //设置输入框的时间
    var startDate = new Date(new Date().getTime() - days * 1000 * 60 * 60 * 24);
    var endDate = new Date(new Date().getTime() - 1 * 1000 * 60 * 60 * 24);
    var dateStr = dateFormat(startDate) + " - " + dateFormat(endDate);
    $('#chooseTime').val(dateStr);
};
function test(event,productName,productId){
     var flag = 1;
     var productId = productId;
     var productName = productName;
     var chooseTime = $("#chooseTime").val();
     var beginDate = chooseTime.split("-")[0];
     var endDate = chooseTime.split("-")[1];
     window.location.href = base + '/productComment/detail?productId='
            + productId + '&productName=' + productName + '&beginDate=' + beginDate + '&endDate='
            + endDate + '&flag=' + flag + '&choose_date=' + chooseTime;
    event.stopPropagation();
}
